﻿using DotNetCommon;
using DotNetCommon.Extensions;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace DotNetCommonAotTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SerializeTest_ToJson();
            SerializeTest_ToJsonFast();
            DeSerializeTest();

            Console.WriteLine("ok");

        }

        public static void SerializeTest_ToJson()
        {
            //切换到了 NewTonsoft
            var json = "{\"Id\":1,\"Name\":\"刘备\"}";
            AssertUtil.BeTrue(new Person { Id = 1, Name = "刘备" }.ToJson() == json, "err");
            AssertUtil.BeTrue(new Person { Id = 1, Name = "刘备" }.ToJson(new System.Text.Json.JsonSerializerOptions { }) == json, "err");
            AssertUtil.BeTrue(new Person { Id = 1, Name = "刘备" }.ToJson(options => { }) == json, "err");

            //切换到 JsonNode.ToJsonString
            var jsonArrayString = "[{\"Id\":1,\"name\":\"刘备\",\"age\":null}]";
            var jsonObjectString = "{\"Id\":1,\"name\":\"刘备\",\"age\":null}";
            AssertUtil.BeTrue(JsonArray.Parse(jsonArrayString).ToJson() == jsonArrayString, "error");
            AssertUtil.BeTrue(JsonObject.Parse(jsonObjectString).ToJson() == jsonObjectString, "error");
            AssertUtil.BeTrue(JsonDocument.Parse(jsonArrayString).ToJson() == jsonArrayString, "error");
            AssertUtil.BeTrue(JsonArray.Parse(jsonArrayString).ToJson(options: null) == "[{\"Id\":1,\"name\":\"\\u5218\\u5907\",\"age\":null}]", "error");
            AssertUtil.BeTrue(JsonObject.Parse(jsonObjectString).ToJson(options: null) == "{\"Id\":1,\"name\":\"\\u5218\\u5907\",\"age\":null}", "error");
            AssertUtil.BeTrue(JsonDocument.Parse(jsonArrayString).ToJson(options: null) == "[{\"Id\":1,\"name\":\"\\u5218\\u5907\",\"age\":null}]", "error");
            AssertUtil.BeTrue(JsonArray.Parse(jsonArrayString).ToJson(otherSettings: (options) => { }) == jsonArrayString, "error");
            AssertUtil.BeTrue(JsonObject.Parse(jsonObjectString).ToJson(otherSettings: null) == jsonObjectString, "error");
            AssertUtil.BeTrue(JsonDocument.Parse(jsonArrayString).ToJson(otherSettings: null) == jsonArrayString, "error");

            //JArray JObject
            AssertUtil.BeTrue(JArray.Parse(jsonArrayString).ToJson() == jsonArrayString, "err");
            AssertUtil.BeTrue(JObject.Parse(jsonObjectString).ToJson() == jsonObjectString, "err");
            AssertUtil.BeTrue(JArray.Parse(jsonArrayString).ToJson(options: null) == jsonArrayString, "err");
            AssertUtil.BeTrue(JObject.Parse(jsonObjectString).ToJson(options: null) == jsonObjectString, "err");
            AssertUtil.BeTrue(JArray.Parse(jsonArrayString).ToJson(otherSettings: null) == jsonArrayString, "err");
            AssertUtil.BeTrue(JObject.Parse(jsonObjectString).ToJson(otherSettings: null) == jsonObjectString, "err");

            //JsonTreeNode
            AssertUtil.BeTrue(JsonTreeNode.Parse(jsonArrayString).ToJson() == jsonArrayString, "err");
            AssertUtil.BeTrue(JsonTreeNode.Parse(jsonArrayString).ToJson(options: null) == "[{\"Id\":1,\"name\":\"\\u5218\\u5907\",\"age\":null}]", "err");
            AssertUtil.BeTrue(JsonTreeNode.Parse(jsonArrayString).ToJson(otherSettings: null) == jsonArrayString, "err");
        }

        public static void SerializeTest_ToJsonFast()
        {
            //切换到了 NewTonsoft
            var json = "{\"Id\":1,\"Name\":\"刘备\"}";
            AssertUtil.BeTrue(new Person { Id = 1, Name = "刘备" }.ToJsonFast() == json, "err");

            //切换到 JsonNode.ToJsonString
            var jsonArrayString = "[{\"Id\":1,\"name\":\"刘备\",\"age\":null}]";
            var jsonObjectString = "{\"Id\":1,\"name\":\"刘备\",\"age\":null}";
            AssertUtil.BeTrue(JsonArray.Parse(jsonArrayString).ToJsonFast() == jsonArrayString, "error");
            AssertUtil.BeTrue(JsonObject.Parse(jsonObjectString).ToJsonFast() == jsonObjectString, "error");
            AssertUtil.BeTrue(JsonDocument.Parse(jsonArrayString).ToJsonFast() == jsonArrayString, "error");

            //JArray JObject
            AssertUtil.BeTrue(JArray.Parse(jsonArrayString).ToJsonFast() == jsonArrayString, "err");
            AssertUtil.BeTrue(JObject.Parse(jsonObjectString).ToJsonFast() == jsonObjectString, "err");

            //JsonTreeNode
            AssertUtil.BeTrue(JsonTreeNode.Parse(jsonArrayString).ToJsonFast() == jsonArrayString, "err");
        }

        public static void DeSerializeTest()
        {
            var jsonArrayString = "[{\"Id\":1,\"Name\":\"刘备\"}]";
            var jsonObjectString = "{\"Id\":1,\"Name\":\"刘备\"}";

            AssertUtil.BeTrue(jsonObjectString.ToObject<Person>().ToJson() == jsonObjectString, "err");

            AssertUtil.BeTrue(jsonObjectString.ToObject<JsonObject>().ToJson() == jsonObjectString, "err");
            AssertUtil.BeTrue(jsonArrayString.ToObject<JsonArray>().ToJson() == jsonArrayString, "err");

            AssertUtil.BeTrue(jsonObjectString.ToObject<JObject>().ToJson() == jsonObjectString, "err");
            AssertUtil.BeTrue(jsonArrayString.ToObject<JArray>().ToJson() == jsonArrayString, "err");

            AssertUtil.BeTrue(jsonObjectString.ToObject<JsonDocument>().ToJson() == jsonObjectString, "err");
            AssertUtil.BeTrue(jsonArrayString.ToObject<JsonTreeNode>().ToJson() == jsonArrayString, "err");
        }
    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
